package com.sss.hloster07;

import java.util.Scanner;

public class JavaExercise39 {

	public static void main(String[] args) {
		test39();
	}

	/**
	 * 编写一个函数，输入n为偶数时，调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数1/1+1/3+...+1/n(利用指针函数)
	 */
	private static void test39() {
		Scanner sc = new Scanner(System.in);
		System.out.println("输入n:");
		int n = sc.nextInt();
		double index = sum(n);
		System.out.println(index);
	}

	private static double sum(int x) {
		double sum = 0;
		if (x % 2 == 0) {
			for (int i = 2; i <= x; i += 2) {
				sum = sum + (double) 1 / i;
			}
		} else {
			for (int i = 1; i <= x; i += 2) {
				sum = sum + (double) 1 / i;
			}
		}
		return sum;
	}
}
